﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Scheduling.Models.Interfaces;

namespace Scheduling.Models.Repos.InMemory
{
    public class InMemoryRoomLoader : IRoomLoader
    {

        private static List<Room> RoomList;


        public InMemoryRoomLoader()
        {
            if (RoomList == null)
            {

                RoomList = new List<Room>();
                Room r1 = new Room() { ID = "21E404", Name = null, Capacity = 32, Description = "" };
                Room r2 = new Room() { ID = "21E403", Name = null, Capacity = 32, Description = "" };

                r1.Properties = new List<RoomProperty>() { new RoomProperty() { Name = "Computers", Value = 16 } };
                r2.Properties = new List<RoomProperty>() { new RoomProperty() { Name = "Computers", Value = 16 } };
                RoomList.Add(r1);
                RoomList.Add(r2);
            }
        }

        public List<Room> GetRooms()
        {
            

            return RoomList;
        }

        public List<Room> SearchRoomsByPatten(string Pattern)
        {
            var rooms = GetRooms();
            return rooms.Where(u => u.ID.Contains(Pattern) || u.Name.Contains(Pattern)).ToList<Room>();
        }

        public bool RoomExists(string RoomID)
        {
            foreach (Room room in RoomList)
            {
                if (room.ID == RoomID)
                    return true;
            }
            return false;
        }

        public Room GetRoom(string RoomID)
        {

            foreach (Room room in RoomList)
            {
                if (room.ID == RoomID)
                    return room;
            }
            return null;
        }

        public void DeleteRoom(string RoomID)
        {
            for (int i = 0; i < RoomList.Count; i++)
            {
                if (RoomList[i].ID == RoomID)
                    RoomList.RemoveAt(i);
            }
        }

        public void InsertRoom(Room room)
        {
            RoomList.Add(room);
        }

        public void EditRoom(Room room)
        {
            DeleteRoom(room.ID);
            InsertRoom(room);
        }

        public void Clear()
        {
            RoomList = null;
        }
    }
}